import domain.ListNode;

/**
 * 3 - > 1 - > 2
 * 6 ->  4  -> 8
 *
 * 9 -> 6 -> 0
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2022-07-06
 */
public class Test链表求和 {
    public static void main(String[] args) {

    }

    public ListNode sumListNode(ListNode head1, ListNode head2) {
        if (head1 == null) {
            return head2;
        }

        if (head2 == null) {
            return head1;
        }

        //先反转
        ListNode reverseHead1 = reverseListNode(head1);
        ListNode reverseHead2 = reverseListNode(head2);

        ListNode result = new ListNode(0);
        ListNode p = result;
        int takeOver = 0;
        while (reverseHead1 != null || reverseHead2 != null) {
            int num1 = reverseHead1 != null ? reverseHead1.val : 0;
            int num2 = reverseHead1 != null ? reverseHead1.val : 0;
            int sum = num1 + num2 + takeOver;
            if (sum >= 10) {
                takeOver = 1;
                sum -= 10;
            }
            p.next = new ListNode(sum);
            p = p.next;

            reverseHead1 = reverseHead1 != null ? reverseHead1.next : null;
            reverseHead2 = reverseHead2 != null ? reverseHead2.next : null;
        }

        return reverseListNode(result.next);
    }

    private ListNode reverseListNode(ListNode head) {
       return  null ;
    }


}
